package name.jiaosha.air.company.service.impl;

import java.util.List;

import name.jiaosha.air.company.dao.IPartDao;
import name.jiaosha.air.company.dao.IPartPackageDao;
import name.jiaosha.air.company.domain.Part;
import name.jiaosha.air.company.domain.PartPackage;
import name.jiaosha.air.company.service.IGoodsService;
import name.jiaosha.air.system.dao.IGoodsDao;
import name.jiaosha.air.system.dao.IProductDao;
import name.jiaosha.air.system.domain.Goods;
import name.jiaosha.air.system.domain.Product;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ali8ba.common.PageInfo;
import com.ali8ba.common.PinyinToolkit;

@Service
public class GoodsService implements IGoodsService {

	@Autowired
	private IPartDao partDao;

	@Autowired
	private IProductDao productDao;

	@Autowired
	private IGoodsDao goodsDao;

	@Autowired
	private IPartPackageDao partPackageDao;

	@Override
	public void createPart(Part part) {

		part.setPinyin(PinyinToolkit.cn2FirstSpell(part.getName()));
		part.setId(null);
		partDao.save(part);

		for (PartPackage item : part.getPackageItems()) {
			item.setPartId(part.getId());
			partPackageDao.save(item);

		}
	}

	@Override
	public Part getPartById(int id) {

		return partDao.get(id);
	}

	@Override
	public void createProduct(Product product) {
		product.setId(null);
		product.setPinyin(PinyinToolkit.cn2FirstSpell(product.getName()));
		productDao.save(product);

	}

	@Override
	public PageInfo<Goods> getGoodsListByComId(Integer comId, int index,
			int pageSize) {
		String jpql = "SELECT g FROM Goods g";

		return goodsDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public PageInfo<Part> getPartListByComId(Integer comId, int index,
			int pageSize) {

		String jpql = "SELECT p FROM Part p";

		return partDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public PageInfo<Product> getProductListByComId(Integer comId, int index,
			int pageSize) {

		String jpql = "SELECT p FROM Product p";

		return productDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public void updatePart(Part part) {

		part.setPinyin(PinyinToolkit.cn2FirstSpell(part.getName()));
		partDao.saveOrUpdate(part);
	}

	@Override
	public void updateProduct(Product product) {

		product.setPinyin(PinyinToolkit.cn2FirstSpell(product.getName()));
		productDao.saveOrUpdate(product);

	}

	@Override
	public List<PartPackage> getPartPackageByPartId(Integer partId) {
		String jpql = "SELECT p FROM PartPackage p where p.partId=?";

		return partPackageDao.find(jpql, partId);
	}

	@Override
	public Product getProductById(int id) {
		
		return productDao.get(id);
	}

	@Override
	public Goods getGoodsById(int id) {
		
		return goodsDao.get(id);
	}

	
	
}
